package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.activity.SelPictureActivity;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.adapter.AlbumPicAdapter;
import flc.ast.bean.AlbumItemBean;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.ActivityAlbumBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.view.container.StkRecycleView;

public class AlbumActivity extends BaseAc<ActivityAlbumBinding> {

    public static String albumId = "";
    private List<AlbumItemBean> listShow = new ArrayList<>();
    private MyAlbumBean currentBean;
    private AlbumPicAdapter picAdapter;
    private boolean isManage;
    private List<String> listSel = new ArrayList<>();
    private AlbumAdapter albumAdapter;
    private Dialog mySelAlbumDialog;
    private Dialog myAddAlbumDialog;
    private EditText dialogAdd;
    private int oldPosition;


    @Override
    protected int onCreate() {
        return R.layout.activity_album;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivAddImage.setOnClickListener(this);
        mDataBinding.tvAddImage.setOnClickListener(this);
        mDataBinding.tvAlbumManageAllSel.setOnClickListener(this);
        mDataBinding.tvAlbumManageCancel.setOnClickListener(this);
        mDataBinding.ivMove.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 4));
        picAdapter = new AlbumPicAdapter();
        mDataBinding.rvPictureList.setAdapter(picAdapter);
        picAdapter.setOnItemClickListener(this);

        picAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                isManage = true;
                picAdapter.setManage(isManage);
                mDataBinding.ivAlbumBack.setVisibility(View.INVISIBLE);
                mDataBinding.ivAddImage.setVisibility(View.INVISIBLE);
                mDataBinding.tvAlbumManageAllSel.setVisibility(View.VISIBLE);
                mDataBinding.tvAlbumManageCancel.setVisibility(View.VISIBLE);
                mDataBinding.llManageBottom.setVisibility(View.VISIBLE);
                return false;
            }
        });

        albumAdapter = new AlbumAdapter();
        albumAdapter.setEdit(true);
        albumAdapter.setOnItemClickListener(this);

        //选择相册弹框
        selAlbumDialog();

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAddImage://添加图片
            case R.id.tvAddImage:
                SelPictureActivity.type = 0;
                startActivity(SelPictureActivity.class);
                break;
            case R.id.tvAlbumManageAllSel://全选
                listSel.clear();
                for (AlbumItemBean bean : listShow) {
                    bean.setSelected(true);
                    listSel.add(bean.getPath());
                }
                picAdapter.notifyDataSetChanged();
                mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
                break;
            case R.id.tvAlbumManageCancel://管理取消
                cancelEdit();
                break;
            case R.id.ivDelete://删除
                deleteAlbum();
                break;
            case R.id.ivMove://移动到
                getAlbumData();
                mySelAlbumDialog.show();
                break;
            //选择相册弹框
            case R.id.tvDialogSelAlbumCancel://取消
                mySelAlbumDialog.dismiss();
                break;
            case R.id.tvDialogSelAlbumConfirm://确定
                if (listSel.size() == 0) return;
                delete();
                List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
                listSp.get(oldPosition - 1).getListData().addAll(listSel);
                AppSpUtils.savePicAlbumList(listSp);
                mySelAlbumDialog.dismiss();
                cancelEdit();
                break;
            //新建相册弹框
            case R.id.ivDialogAddAlbumConfirm://确定
                addAlbum();
                break;
            case R.id.ivDialogAddAlbumCancel://取消
                myAddAlbumDialog.dismiss();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == picAdapter) {
            if (isManage) {
                if (picAdapter.getItem(position).isSelected()) {
                    picAdapter.getItem(position).setSelected(false);
                    listSel.remove(picAdapter.getItem(position).getPath());
                } else {
                    picAdapter.getItem(position).setSelected(true);
                    listSel.add(picAdapter.getItem(position).getPath());
                }
                picAdapter.notifyItemChanged(position);
                mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
            }
        } else if (adapter == albumAdapter) {
            if (position == 0) {
                //新建相册弹框
                addAlbumDialog();
                myAddAlbumDialog.show();
                return;
            }
            albumAdapter.getItem(oldPosition).setSelected(false);
            oldPosition = position;
            albumAdapter.getItem(position).setSelected(true);
            albumAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvDelete.setText(getString(R.string.delete_num, 0));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (MyAlbumBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    currentBean = bean;
                    mDataBinding.tvAlbumTitle.setText(bean.getName());
                    for (String listDatum : bean.getListData()) {
                        listShow.add(new AlbumItemBean(listDatum, false));
                    }
                    break;
                }
            }
        }
        if (listShow.size() > 0) {
            picAdapter.setList(listShow);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.llPictureNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.llPictureNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除相册图片
    private void deleteAlbum() {
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (MyAlbumBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    bean.getListData().removeAll(listSel);
                    break;
                }
            }
            AppSpUtils.savePicAlbumList(listSp);
        }
        cancelEdit();
    }

    //删除相册图片
    private void delete() {
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (MyAlbumBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    bean.getListData().removeAll(listSel);
                    break;
                }
            }
            AppSpUtils.savePicAlbumList(listSp);
        }
    }

    private void cancelEdit() {
        isManage = false;
        picAdapter.setManage(isManage);
        getData();
        mDataBinding.ivAlbumBack.setVisibility(View.VISIBLE);
        mDataBinding.ivAddImage.setVisibility(View.VISIBLE);
        mDataBinding.tvAlbumManageAllSel.setVisibility(View.INVISIBLE);
        mDataBinding.tvAlbumManageCancel.setVisibility(View.INVISIBLE);
        mDataBinding.llManageBottom.setVisibility(View.GONE);
        listSel.clear();
    }

    private void getAlbumData() {
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.add(0, new MyAlbumBean());
            albumAdapter.setList(listSp);
        }
    }

    private void addAlbum() {
        String albumName = dialogAdd.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myAddAlbumDialog.dismiss();
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        listSave.add(new MyAlbumBean(TimeUtils.getNowString(), albumName, "", new ArrayList<>(), false));
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.savePicAlbumList(listSp);
        } else {
            AppSpUtils.savePicAlbumList(listSave);
        }
        getAlbumData();
    }

    //选择相册弹框
    private void selAlbumDialog() {
        mySelAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_sel_album, null);
        mySelAlbumDialog.setContentView(inflate);
        mySelAlbumDialog.setCancelable(true);
        Window dialogWindow = mySelAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView title = inflate.findViewById(R.id.tvDialogSelAlbumTitle);
        TextView right = inflate.findViewById(R.id.tvDialogSelAlbumConfirm);
        TextView cancel = inflate.findViewById(R.id.tvDialogSelAlbumCancel);
        StkRecycleView recycleView = inflate.findViewById(R.id.rvAlbumList);
        recycleView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recycleView.setAdapter(albumAdapter);
        title.setText(getString(R.string.move_to));
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //新建相册弹框
    private void addAlbumDialog() {
        myAddAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_album, null);
        myAddAlbumDialog.setContentView(inflate);
        myAddAlbumDialog.setCancelable(true);
        Window dialogWindow = myAddAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogAddAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogAddAlbumCancel);
        dialogAdd = inflate.findViewById(R.id.etDialogAddAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }


}